package cn.nkjobsearch.html;

import java.io.BufferedReader;
import java.io.FileReader;

import cn.nkjobsearch.config.Config;
import cn.nkjobsearch.config.ConfigChinaHR;
import cn.nkjobsearch.publicClass.File;

public class ComChinaHRController {
	public ComChinaHRController() {
	}

	public void startGetID() {
		readChinaHR_PIC();
		ComChinaHRSearchList rm = new ComChinaHRSearchList();
		rm.start(); 
		while (true) {
			/**
			 * 程序结束
			 */
			if(!ConfigChinaHR.CHINAHR_SEARCHLIST_RUNNING) {
				writeChinaHR_PIC();
				break;
			}
			ConfigChinaHR.CHINAHR_SEARCHLIST_TIMER++;
			if (ConfigChinaHR.CHINAHR_SEARCHLIST_TIMER >= ConfigChinaHR.CHINAHR_SEARCHLIST_DELAY_SECONDS) {
				rm.close();
				rm.stop();
				writeChinaHR_PIC();
				ConfigChinaHR.CHINAHR_SEARCHLIST_TIMER = 0;
				System.out.println("interrupt");
				rm = new ComChinaHRSearchList();
				rm.start();
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void startGetHTML() {
		ComChinaHRContent c = new ComChinaHRContent();
		c.start();
		while (true) {
			/**
			 * 程序结束
			 */
			if(!ConfigChinaHR.CHINAHR_CONTENT_RUNNING) {
				break;
			}
			ConfigChinaHR.CHINAHR_CONTENT_TIMER++;
			if(ConfigChinaHR.CHINAHR_CONTENT_TIMER >= ConfigChinaHR.CHINAHR_CONTENT_DELAY_SECONDS) {
				c.close();
				c.stop();
				ConfigChinaHR.CHINAHR_CONTENT_TIMER = 0;
				System.out.println("Interrupt: GET Content Of ChinaHR.");
				c = new ComChinaHRContent();
				c.start();
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void writeChinaHR_PIC() {
		String confStr = ConfigChinaHR.CHINAHR_SEARCHLIST_P + "\r\n"
				+ ConfigChinaHR.CHINAHR_SEARCHLIST_I + "\r\n"
				+ ConfigChinaHR.CHINAHR_SEARCHLIST_C + "\r\n";
		File.writeConf(ConfigChinaHR.CHINAHR_SEARCHLIST_PIC_CONF_PATH, confStr);
	}

	private void readChinaHR_PIC() {
		try {
			BufferedReader bufferedReader = null;
			bufferedReader = new BufferedReader(new FileReader(
					ConfigChinaHR.CHINAHR_SEARCHLIST_PIC_CONF_PATH));
			ConfigChinaHR.CHINAHR_SEARCHLIST_P = Integer.parseInt(bufferedReader
					.readLine().toString());
			ConfigChinaHR.CHINAHR_SEARCHLIST_I = Integer.parseInt(bufferedReader
					.readLine().toString());
			ConfigChinaHR.CHINAHR_SEARCHLIST_C = Integer.parseInt(bufferedReader
					.readLine().toString());
			bufferedReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
